import axios from 'axios';

function getLocation() {
  return new Promise((resolve, reject) => {
    try {
      let curCityInfo = JSON.parse(localStorage.getItem('hkzf_city_56'));

      if (curCityInfo) {
        resolve(curCityInfo);
      } else {
        var myCity = new window.BMap.LocalCity();
        myCity.get(async (result) => {
          const res = await axios.get(`http://localhost:8080/area/info`, {
            params: {
              name: result.name
            }
          });
          resolve(res.data.body);
          localStorage.setItem('hkzf_city_56', JSON.stringify(res.data.body));
        });

      }
    } catch (e) {
      reject(e);
    }

  });
}

export {
  getLocation
};
